/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 30318
 * Date: 2023-11-21
 * Time: 16:29
 */
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

class IntegralQuery{
    public static int readMemberIntergral() {//查询会员卡号相对应的积分
        System.out.println("请输入您的会员号：");
        Scanner in = new Scanner(System.in);
        int inputMemberId = in.nextInt(); // 输入的会员号
        boolean found = false; // 是否找到会员号
        String foundMemberId = "";
        int foundMemberIntegral = 0;
        try (BufferedReader br = new BufferedReader(new FileReader("memberInfo.txt"))) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] parts = line.split("\t");
                if (parts.length > 0 && Integer.parseInt(parts[0]) == inputMemberId) {
                    found = true;
                    foundMemberIntegral = Integer.parseInt(parts[2]);
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(found == false)
            return -1;
        return foundMemberIntegral;
    }
}
class ChangeGoods {
    IntegralQuery integralQuery=new IntegralQuery();
    TestOfMember testOfMember =new TestOfMember();
    public void PointExchange(){
        int memberInteral = integralQuery.readMemberIntergral();
        if(memberInteral == -1) {
            System.out.println("未找到该会员！");
        }
        else {
            System.out.println("您当前的积分为：" + memberInteral);
            System.out.println("*************   积分兑换   *************");
            System.out.println("输入1： 对于积分满500的客户，可换购百事可乐饮料2瓶");
            System.out.println("输入2： 对于积分满1000的客户，可换购台灯1个");
            System.out.println("输入3： 对于积分满1500的客户，可换购10kg米1袋");
            System.out.println("输入4： 对于积分满2000的客户，可换购炒锅1个");
            System.out.println("输入5： 对于积分满2500的客户，可换购空气炸锅1个");
            System.out.println("输入0：不换购");
            System.out.println("请选择：");
            Scanner in=new Scanner(System.in);
            int num=in.nextInt();
            switch (num) {
                case 1:
                    if (memberInteral >= 500) {
                        System.out.println("请输入您的会员号：");
                        if (testOfMember.queryMember(in.next())) {
                            String oldstr = testOfMember.toString();
                            int newpoint = 500;
                            int oldpoint = testOfMember.getMember().getMemberIntegral();
                            testOfMember.getMember().setMemberIntegral(oldpoint - newpoint);//积分修改
                            String newstr = testOfMember.toString();
                            testOfMember.modify(oldstr, newstr);
                            System.out.println("成功兑换百事可乐2瓶");
                        }
                    } else
                        System.out.println("积分不满足兑换条件");
                    break;
                case 2:
                    if (memberInteral >= 1000) {
                        System.out.println("请输入您的会员号：");
                        if (testOfMember.queryMember(in.next())) {
                            String oldstr = testOfMember.toString();
                            int newpoint = 1000;
                            int oldpoint = testOfMember.getMember().getMemberIntegral();
                            testOfMember.getMember().setMemberIntegral(oldpoint - newpoint);//积分修改
                            String newstr = testOfMember.toString();
                            testOfMember.modify(oldstr, newstr);
                            System.out.println("成功兑换台灯1个");
                        }
                    } else {
                        System.out.println("积分不满足兑换条件");
                    }
                    break;
                case 3:
                    if (memberInteral >= 1500) {
                        System.out.println("请输入您的会员号：");
                        if (testOfMember.queryMember(in.next())) {
                            String oldstr = testOfMember.toString();
                            int newpoint = 1500;
                            int oldpoint = testOfMember.getMember().getMemberIntegral();
                            testOfMember.getMember().setMemberIntegral(oldpoint - newpoint);//积分修改
                            String newstr = testOfMember.toString();
                            testOfMember.modify(oldstr, newstr);
                            System.out.println("成功兑换10kg米1袋");
                        }
                    } else {
                        System.out.println("积分不满足兑换条件");
                    }
                    break;
                case 4:
                    if (memberInteral>= 2000) {
                        if (testOfMember.queryMember(in.next())) {
                            String oldstr = testOfMember.toString();
                            int newpoint = 1500;
                            int oldpoint = testOfMember.getMember().getMemberIntegral();
                            testOfMember.getMember().setMemberIntegral(oldpoint - newpoint);//积分修改
                            String newstr = testOfMember.toString();
                            testOfMember.modify(oldstr, newstr);
                            System.out.println("成功兑换炒锅1个");
                        }
                    } else {
                        System.out.println("积分不满足兑换条件");
                    }
                    break;
                case 5:
                    if (memberInteral >= 2500) {
                        if (testOfMember.queryMember(in.next())) {
                            String oldstr = testOfMember.toString();
                            int newpoint = 1500;
                            int oldpoint = testOfMember.getMember().getMemberIntegral();
                            testOfMember.getMember().setMemberIntegral(oldpoint - newpoint);//积分修改
                            String newstr = testOfMember.toString();
                            testOfMember.modify(oldstr, newstr);
                            System.out.println("成功兑换空气炸锅1个");
                        }
                    } else {
                        System.out.println("积分不满足兑换条件");
                    }
                    break;
                case 0:
                    System.out.println("暂不兑换");
                    break;
                default:
                    System.out.println("您输入有误");
            }
        }
    }
}